#include <iostream>
#include <libssh/libssh.h>
#include <util.h>
// 检查 SSH 连接状态并输出错误信息
void check_ssh_status(ssh_session session, int rc) {
    if (rc != SSH_OK) {
        std::cerr << "SSH error: " << ssh_get_error(session) << std::endl;
        ssh_finalize();
        exit(EXIT_FAILURE);
    }
}

int openssh() {
    ssh_session session;
    ssh_channel channel;
    int rc;

    // 初始化 SSH 会话
    session = ssh_new();
    if (session == NULL) {
        std::cerr << "Failed to create SSH session." << std::endl;
        return EXIT_FAILURE;
    }

// 设置要连接的远程主机名
    rc = ssh_options_set(session, SSH_OPTIONS_HOST, "192.168.10.253");
    if (rc != SSH_OK) {
        fprintf(stderr, "Error setting host: %s\n", ssh_get_error(session));
        ssh_free(session);
        return -1;
    }

    // 设置连接的远程端口
    int port = 22;
    rc = ssh_options_set(session, SSH_OPTIONS_PORT, &port);
    if (rc != SSH_OK) {
        fprintf(stderr, "Error setting port: %s\n", ssh_get_error(session));
        ssh_free(session);
        return -1;
    }

    // 设置登录远程主机使用的用户名
    rc = ssh_options_set(session, SSH_OPTIONS_USER, "root");
    if (rc != SSH_OK) {
        fprintf(stderr, "Error setting user: %s\n", ssh_get_error(session));
        ssh_free(session);
        return -1;
    }

    // 连接到 SSH 服务器
    rc = ssh_connect(session);
    check_ssh_status(session, rc);

       // 4. 指定私钥文件路径
       const char* private_key_path = "/root/.ssh/linux";
       const char* passphrase = nullptr;  // 如果私钥有密码，这里填写密码
        ssh_key private_key = nullptr;
       // 导入私钥
       rc = ssh_pki_import_privkey_file(private_key_path, passphrase, nullptr, nullptr, &private_key);
       if (rc != SSH_OK) {
           std::cerr << "Failed to import private key: " << ssh_get_error(session) << std::endl;
           ssh_disconnect(session);
           ssh_free(session);
           return EXIT_FAILURE;
       }

       // 进行密钥认证
       rc = ssh_userauth_publickey(session, nullptr, private_key);
       if (rc != SSH_AUTH_SUCCESS) {
           std::cerr << "Authentication failed: " << ssh_get_error(session) << std::endl;
           ssh_key_free(private_key);
           ssh_disconnect(session);
           ssh_free(session);
           return EXIT_FAILURE;
       }

       std::cout << "Authentication successful!" << std::endl;

       // 释放私钥
       ssh_key_free(private_key);










    // 进行密码认证
   // rc = ssh_userauth_password(session, NULL, "1813033378");
  //  check_ssh_status(session, rc);

    // 打开一个 SSH 通道
    channel = ssh_channel_new(session);
    if (channel == NULL) {
        std::cerr << "Failed to create SSH channel." << std::endl;
        ssh_disconnect(session);
        ssh_free(session);
        return EXIT_FAILURE;
    }

    // 打开通道
    rc = ssh_channel_open_session(channel);
    check_ssh_status(session, rc);

    // 执行命令
    rc = ssh_channel_request_exec(channel, "ls -l");
    check_ssh_status(session, rc);

    // 读取命令输出
    char buffer[256];
    int nbytes;
    while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) {
        std::cout.write(buffer, nbytes);
    }

    // 检查是否读取失败
    if (nbytes < 0) {
        std::cerr << "Error reading from SSH channel." << std::endl;
    }

    // 关闭通道
    ssh_channel_send_eof(channel);
    ssh_channel_close(channel);
    ssh_channel_free(channel);

    // 断开 SSH 连接
    ssh_disconnect(session);
    ssh_free(session);

    // 清理 libssh 库
    ssh_finalize();

    return EXIT_SUCCESS;
}
